﻿using System;
using System.Collections.Generic;
using System.Linq;
using DAL = IrisGenerator.Common.DatabaseModel.DataAccess;
using System.Text;
using System.Data;
using IrisGenerator.Common.Tools;

namespace IrisGenerator.Common.DatabaseModel
{
    public class ConstraintKey
    {
        private Guid? _constraintId;
        private Guid? _primaryColumnId;
        private Guid? _foreignColumnId;

        private Constraint _constraint = null;
        private Column _primaryColumn = null;
        private Column _foreignColumn = null;

        public Guid? Identifier { get; set; }
        public Constraint Constraint
        {
            get
            {
                if (_constraint == null && _constraintId.HasValue)
                    _constraint = Constraint.Load(_constraintId);
                return _constraint;
            }
            set
            {
                _constraint = value;
                if (value != null)
                    _constraintId = value.Identifier;
                else
                    _constraintId = null;
            }
        }
        public Column PrimaryColumn {
            get
            {
                if (_primaryColumn == null && _primaryColumnId.HasValue)
                    _primaryColumn = Column.Load(_primaryColumnId);
                return _primaryColumn;
            }
            set
            {
                _primaryColumn = value;
                if (value != null)
                    _primaryColumnId = value.ColId;
                else
                    _primaryColumnId = null;
            }

        }
        public Column ForeignColumn {
            get
            {
                if (_foreignColumn == null && _foreignColumnId.HasValue)
                    _foreignColumn = Column.Load(_foreignColumnId);
                return _foreignColumn;
            }
            set
            {
                _foreignColumn = value;
                if (value != null)
                    _foreignColumnId = value.ColId;
                else
                    _foreignColumnId = null;

            }
        }

        public virtual Guid? Insert()
        {
            DAL.ConstraintKey dbo = null;
            try
            {
                if (this.ForeignColumn == null)
                    this.ForeignColumn = new Column();
                dbo = new DAL.ConstraintKey();
                return dbo.ConstraintKey_Insert(this.Constraint.Identifier,this.PrimaryColumn.ColId,this.ForeignColumn.ColId);
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
            return null;
        }

        internal static ConstraintKey Load(DataRow dr)
        {
            ConstraintKey ck = new ConstraintKey();
            
            if ((dr["CONS_KEY_ID"] != System.DBNull.Value))
            {
                ck.Identifier = ((System.Nullable<System.Guid>)(dr["CONS_KEY_ID"]));
            }

            if((dr["CONS_ID"]!=System.DBNull.Value))
            {
                ck._constraintId = ((System.Nullable<System.Guid>)(dr["CONS_ID"]));
                
            }

            if ((dr["CONS_KEY_PRIMARY"] != System.DBNull.Value))
            {
                ck._primaryColumnId = ((System.Nullable<System.Guid>)(dr["CONS_KEY_PRIMARY"]));

            }

            if ((dr["CONS_KEY_FOREIGN"] != System.DBNull.Value))
            {
                ck._foreignColumnId = ((System.Nullable<System.Guid>)(dr["CONS_KEY_FOREIGN"]));

            }
            return ck;
        }

        private void Clean()
        {
            this.Identifier = null;
            this.Constraint = null;
            this.ForeignColumn = null;
            this.PrimaryColumn = null;
        }

        public static ConstraintKeyCollection GetConstraintKeys(Constraint cons)
        {
            DAL.ConstraintKey dbo = null;
            ConstraintKeyCollection col = new ConstraintKeyCollection();
            try
            {
                dbo = new DAL.ConstraintKey();
                DataSet ds = dbo.GetConstraintKeys(cons.Identifier);
                if (GlobalTools.IsSafeDataSet(ds))
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        ConstraintKey ck = ConstraintKey.Load(dr);
                        col.Add(ck);
                    }
                }
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
            return col;
        }
    }
}
